package org.devocean.groupbuying.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.devocean.groupbuying.dao.FormulaDAO;
import org.devocean.groupbuying.domain.Formula;
import org.springframework.stereotype.Repository;

@Repository
public class FormulaDAOImpl implements FormulaDAO {

	@PersistenceContext
	private EntityManager em;

	public List<Formula> findAllFormulas() {

		return em.createQuery("select f from Formula f",Formula.class).getResultList();
	}

	public long countFormula() {
		return em.createQuery("select count(f) from Formula f", long.class)
				.getSingleResult();
	}

	public Formula findFormulaById(Integer formulaID) {
		return em.find(Formula.class, formulaID);
	}

	public void deleteFormula(Integer formulaID) {
		final Formula formula = em.find(Formula.class, formulaID);
		if (formula != null) {
			em.remove(formula);
		}

	}

	public Formula saveFormula(Formula formula) {
		em.persist(formula);
		return formula;
	}

	public Formula updateFormula(Formula formula) {
		em.merge(formula);
		return formula;
	}

}
